import 'package:flutter/material.dart';
import 'package:flutter_base/base/pop_base_state.dart';
import 'package:flutter_base/common/sp_key.dart';
import 'package:flutter_base/page/common/user_protocol_page.dart';
import 'package:flutter_base/splash_page.dart';
import 'package:flutter_base/utils/log_utils.dart';
import 'package:flutter_base/utils/navigator_utils.dart';
import 'package:flutter_base/utils/sp_utils.dart';
import 'package:flutter_base/welcome_page.dart';

import 'common/user_helper.dart';
import 'net/dio_utils.dart';
import 'net/http_helper.dart';

///
/// @DIR_PATH：lib
/// @TIME：2023/11/10 15:02
/// @AUTHOR：starr
/// @DESC：首页
///
class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  State<StatefulWidget> createState() => _IndexPageState();
}

class _IndexPageState extends PopBaseState<IndexPage> {
  //用户是否第一次使用
  bool? _userFirst = false;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      //检查权限
      initData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      ///层叠布局
      body: Stack(
        children: [
          ///构建背景
          Positioned(
            left: 0,
            right: 0,
            top: 0,
            bottom: 0,
            child: Image.asset(
              "assets/images/3.0x/mine_bg.png",
              fit: BoxFit.fill,
            ),
          ),
        ],
      ),
    );
  }

  ///第三方功能初始化
  ///获取保存的用户偏好设置
  void initData() async {
    //获取当前的运行环境
    //当App运行在Release环境时，inProduction为true；
    //当App运行在Debug和Profile环境时，inProduction为false。
    const bool inProduction = bool.fromEnvironment("dart.vm.product");
    //为ture时输出日志
    const bool isLog = !inProduction;

    //初始化本地存储工具
    await SPUtils.getInstance();
    //初始化日志工具
    LogUtils.init(tag: "flutter_log", isDebug: isLog);
    //获取用户是否第一次登录
    _userFirst=await SPUtils.getBool(spUserIsFirstKey);
    //获取用户隐私协议的状态
    bool? userProtocol = await SPUtils.getBool(spUserProtocolKey);
    LogUtils.e("用户是否同意隐私协议 $userProtocol");
    //记录
    UserHelper.getInstance.userProtocol = userProtocol;
    //初始化用户的登录信息
    UserHelper.getInstance.init();
    //下一步
    openUserProtocol();
    //测试界面
    // NavigatorUtils.openPageByFade(context, SplashPage(), isReplace: true);
  }

  ///判断用户隐私协议
  void openUserProtocol() {
    //已同意用户隐私协议 下一步
    if (UserHelper.getInstance.isUserProtocol != null &&
        UserHelper.getInstance.isUserProtocol == true) {
      openNext();
    } else {
      //未同意用户协议 弹框显示
      showUserProtocolPage(
        context: context,
        dismissCallback: (value) {
          UserHelper.getInstance.userProtocol = false;
          openNext();
        },
      );
    }
  }

  ///进入首页面或者是引导页面
  void openNext() async {
    //网络请求获取APP的配置信息
    ResponseInfo responseInfo =
        await DioUtils.getRequest(url: HttpHelper.USER_INFO);
    if (responseInfo.success) {
      //解析数据
      // AppSettingBean settingBean = AppSettingBean.fromMap(responseInfo.data);
      //配置APP主题
      // if (settingBean.appThemFlag == 1) {
      //   //将APP设置成灰色主题
      //   rootStreamController.add(GlobalBean(100, Colors.grey));
      // }
    }
    //获取配置信息
    if (_userFirst == null || _userFirst == false) {
      ///第一次进入 隐藏logo 显示左右滑动的引导
      SPUtils.save(spUserBeanKey, true);
      NavigatorUtils.openPageByFade(context, SplashPage(), isReplace: true);
    } else {
      ///非第一次 隐藏logo 显示欢迎
      NavigatorUtils.openPageByFade(context, WelcomePage(), isReplace: true);
    }
  }
}
